﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Common;
using GtsServer.Model;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// EventMouldAjax 的摘要说明
    /// </summary>
    public class EventMouldAjax : IHttpHandler
    {
        EventMouldController controller = new EventMouldController();
        EventChanceController eventChanceController = new EventChanceController();
        ChanceTypeController chanceTypeController = new ChanceTypeController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetListByTypeName":
                        GetListByTypeName(context);
                        break;
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 获取事件道具中的事件选项
        /// </summary>
        /// <param name="context"></param>
        private void GetListByTypeName(HttpContext context)
        {
            context.Response.Write(JsonHelper.ListToJson<EventChanceMould>(controller.GetListByEventTypeName()));
        }

        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());

            if (eventChanceController.DeleteByEventMouldID(id))
                context.Response.Write(controller.Delete(id));
            else
                context.Response.Write(false);
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            int sortNum = Convert.ToInt32(context.Request["sortNum"].ToString());
            string name = context.Request["name"];
            int clientToButt = Convert.ToInt32(context.Request["clientToButt"].ToString());
            EventChanceMould model = new EventChanceMould()
            {
                ID = id,
                Name = name,
                SortNum = sortNum,
                ClientToButt = clientToButt
            };
            context.Response.Write(controller.Update(model));
        }

        private void Add(HttpContext context)
        {
            int eventID = Convert.ToInt32(context.Request["eventID"].ToString());
            string name = context.Request["name"];
            int sortNum = Convert.ToInt32(context.Request["sortNum"].ToString());
            int clientToButt = Convert.ToInt32(context.Request["clientToButt"].ToString());
            EventChanceMould model = new EventChanceMould()
            {
                EventID = eventID,
                Name = name,
                SortNum = sortNum,
                ClientToButt = clientToButt
            };
            int id = controller.Add(model);
            List<ChanceType> chanceTypeList = chanceTypeController.GetList();
            foreach (var item in chanceTypeList)
            {
                EventChance eventChanceModel = new EventChance()
                {
                    ChanceTypeID = item.ID,
                    ChanceValue = 0,
                    EventID = eventID,
                    EventMouldID = id
                };
                eventChanceController.Add(eventChanceModel);
            }

            context.Response.Write(id);
        }

        private void GetList(HttpContext context)
        {
            int eventID = Convert.ToInt32(context.Request["eventID"].ToString());
            context.Response.Write(JsonHelper.ListToJson<EventChanceMould>(controller.GetList(eventID)));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}